﻿/*
 * Project	    : Outlook 2003 Style Navigation Pane
 *
 * Author       : Muhammed ŞAHİN
 * eMail        : muhammed.sahin@gmail.com
 *
 * Description  : NavigateBar caption band
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WP.Controls.OutlookStyleNavigateBar
{
    #region Class : NavigateBarCaption

    [Browsable(false)]
    [ToolboxItem(false)]
    class NavigateBarCaption : UserControl
    {

        #region Caption
        private string caption = "";
        public string Caption
        {
            get { return caption; }
            set
            {
                caption = value;
                Invalidate();
            }
        }
        #endregion

        #region Image
        Image image = null;
        public Image Image
        {
            get { return image; }
            set { image = value; }
        }
        #endregion

        #region NavigateBar
        NavigateBar navigateBar = null;
        internal NavigateBar NavigateBar
        {
            get { return navigateBar; }
            set
            {
                navigateBar = value;
                Invalidate();
            }
        }
        #endregion

        #region CollapseButton
        public NavigateBarCollapseButton CollapseButton
        {
            get { return collapseButton; }
        }
        #endregion

        #region CollapseMode
        private bool collapseMode = true;
        public bool CollapseMode
        {
            get { return collapseMode; }
            set
            {
                collapseMode = value;
                CollapseButton.IsExpandArrow = !collapseMode;
                CollapseButton.Invalidate();
            }
        }
        #endregion

        NavigateBarCollapseButton collapseButton;

        #region Constructor Method
        public NavigateBarCaption(NavigateBar tNavigateBar)
        {

            navigateBar = tNavigateBar;
            // Control
            this.Height = SystemInformation.CaptionHeight;// 24;
            this.Dock = DockStyle.Top;
            this.ResizeRedraw = true;
            this.Invalidated += new InvalidateEventHandler(NavigateBarCaption_Invalidated);

            // Collapse Button
            collapseButton = new NavigateBarCollapseButton(navigateBar);
            this.CollapseMode = true;
            collapseButton.Left = this.Width - collapseButton.Size.Width - 1;
            collapseButton.Top = (this.Height - collapseButton.Size.Height) / 2;
            collapseButton.Click += new EventHandler(CollapseButton_Click);
            collapseButton.MouseLeave += new EventHandler(CollapseButton_MouseLeave);
            Controls.Add(collapseButton);

        }

        void CollapseButton_MouseLeave(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        void CollapseButton_Click(object sender, EventArgs e)
        {
            this.Invalidate();
            navigateBar.ChangeCollapseMode(collapseMode);
        }

        void NavigateBarCaption_Invalidated(object sender, InvalidateEventArgs e)
        {
            collapseButton.Invalidate();
        }

        #endregion

        #region Overrided Method

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            PaintThisControl();
        }

        protected override void OnResize(EventArgs e)
        {
            if (collapseButton != null)
                collapseButton.Left = this.Width - 20;

            Invalidate();
            base.OnResize(e);
        }
        #endregion

        #region Other Metods
        void PaintThisControl()
        {

            // Gradient olarak boyama işlemi // Paint gradient

            NavigateBarHelper.PaintGradientControl(this,
                NavigateBar.NavigateBarColorTable.CaptionBegin,
                NavigateBar.NavigateBarColorTable.CaptionEnd);

            //

            using (Graphics g = this.CreateGraphics())
            {

                Pen penBorder = new Pen(navigateBar.NavigateBarColorTable.BorderColor);

                // Image

                if (image != null) g.DrawImage(image, new Rectangle(4, (Height - 10) / 2, 32, 32));

                // Yazıyı yazma  // Draw caption text

                if (!string.IsNullOrEmpty(Caption))
                {
                    g.DrawString(Caption,
                        new Font(SystemFonts.CaptionFont.Name, 10, FontStyle.Bold),
                        new SolidBrush(navigateBar.NavigateBarColorTable.CaptionTextColor),
                        image == null ? 8 : 24, (Height - SystemFonts.DialogFont.GetHeight()) / 4);
                }

                // Etrafın çizgisi // Draw rectangle

                if (CollapseMode) // Sadece başlık durumunda // only caption
                    if (navigateBar.SelectedButton != null)
                        g.DrawRectangle(penBorder, new Rectangle(0, 0, Width - 1, Height - (navigateBar.SelectedButton.IsShowCaptionDescription ? 0 : 1)));
                    else
                        g.DrawRectangle(penBorder, new Rectangle(0, 0, Width - 1, Height - 1));
                else // Başlık ve Daraltılmış durumda // Collapse and caption
                    g.DrawRectangle(penBorder, new Rectangle(-1, 0, Width + 1, Height - 1));

            }

        }
        #endregion

    }

    #endregion
}
